#include "monitor.h"
#include "ui_monitor.h"

//监控
Monitor::Monitor(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Monitor)
{
    ui->setupUi(this);
    udpSocket=new QUdpSocket(this);
    //虚拟机ip
//    address.setAddress("192.168.50.66");
    //port=8888;
    //QHostAddress targetAddr(address);
    //定时器
    tim = new QTimer();
    //tim->setInterval(20);
    connect(tim,SIGNAL(timeout()),this,SLOT(onTimeOut()));//定时器

    qDebug() << udpSocket;
    connect(udpSocket,SIGNAL(readyRead()),this,SLOT(udp_receive_data()));
    connect(ui->ButtonOpenVideo_2,SIGNAL(clicked()),this,SLOT(sendSlot()));//打开视频
    connect(ui->ButtonPicture_2,SIGNAL(clicked()),this,SLOT(Button_PictureSlot()));//截屏
    connect(ui->ButtonSave_2,SIGNAL(clicked()),this,SLOT(ButtonSaveSlot()));//保存

    connect(ui->pushButton_connect,SIGNAL(clicked()),this,SLOT(btnconncetSlot()));
}

Monitor::~Monitor()
{
    delete ui;
}

void Monitor::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix(":/new/prefix2/picture_tubiao/beijing2.png");
    painter.drawPixmap(0, 0, pix);
}

void Monitor::Button_PictureSlot()
{
    tim->stop();
    //固定图片位置.设置大小适应框图及显示
    QPixmap pixmap = QPixmap::fromImage(currentPic);
    int with = ui->labelVideo_2->width();
    int height = ui->labelVideo_2->height();
    fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio,Qt::SmoothTransformation);// 饱满填充
    ui->labelPic_2->setPixmap(fitpixmap);
    ui->labelPic_2->show();
    tim->start();//重新开启定时
    emit changeBackGround();
}

void Monitor::ButtonSaveSlot()
{
    QMessageBox::StandardButton result =QMessageBox::question(this,"保存","是否保存？");
    if(result==QMessageBox::Yes)
    {

        QDateTime datename;
        // 以当前时间命名
        QString timename =datename.currentDateTime().toString("yyyy.MM.dd.HH.mm.ss");
//        qDebug() << timename;
        QString  filename = "D:/pic/"+timename+".jpg";      //保存在电脑D盘
        qDebug() << filename;
        QString str = "所有文件(*.*);;图片(*.jpg *.png)";
        QString path = QFileDialog::getSaveFileName(this,"保存",filename,str);
        currentPic.save("D:/pic/"+timename+".jpg");

        //        QFile writeFile(path);
        //        writeFile.open(QIODevice::WriteOnly|QIODevice::Append|QIODevice::Text);
        //        writeFile.write(currentDatagram);
        //        writeFile.flush();//清空缓存区
        //        writeFile.close();//关闭输出流
    }
    emit changeBackGround();
}

void Monitor::sendSlot()
{
    char buffer[1024]={"get_stream"};
    udpSocket->writeDatagram(buffer,sizeof(buffer),address,port);
    tim->start(40);
    qDebug() << "发送数据：" << buffer ;
    emit changeBackGround();
}

void Monitor::udp_receive_data()
{
    if(udpSocket->hasPendingDatagrams()) //hasPendingDatagrams() 检查是否有待处理的数据报可用
    {
        QByteArray datagram;//创建处理二进制字符数组

        datagram.resize(udpSocket->pendingDatagramSize());
        //接收数据报
        udpSocket->readDatagram(datagram.data(),datagram.size(),&address,&port);//readDatagram()读取数据报的内容
        QImage image;
        image.loadFromData(datagram);
        //固定图片位置.设置大小适应框图及显示
        QPixmap pixmap = QPixmap::fromImage(image);
        int with = ui->labelVideo_2->width();
        int height = ui->labelVideo_2->height();
        QPixmap fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio,Qt::SmoothTransformation);// 饱满填充
        ui->labelVideo_2->setPixmap(fitpixmap);
        ui->labelVideo_2->show();
        //QPixmap current; // 假设 'current' 是用于存储当前图像的 QPixmap 变量
        //current =fitpixmap;
//        qDebug() << "视频显示成功";
        currentPic = image;
    }
}

void Monitor::onTimeOut()
{
    udpSocket->writeDatagram("get_stream",10,address,port);  //发送函数
}

void Monitor::btnconncetSlot()
{

    address.setAddress(ui->lineEdit_ip->text());
    port = ui->lineEdit_duankou->text().toInt();

    qDebug() << "IP：" << address.toString();
    qDebug() << "端口：" << port;
}


